package com.gk.panda.apis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.panda.apis.dto.PasswordDto;
import com.gk.panda.apis.dto.PersonalDataDto;
import com.gk.panda.apis.dto.UserSearchDto;
import com.gk.panda.apis.dto.UserUpdateDto;
import com.gk.panda.apis.mapper.UserMapper;
import com.gk.panda.apis.service.FdfsService;
import com.gk.panda.apis.service.OrgService;
import com.gk.panda.apis.service.UserService;
import com.gk.panda.commons.util.UserUtil;
import com.gk.panda.pojo.Org;
import com.gk.panda.pojo.UrlInfo;
import com.gk.panda.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Base64;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private OrgService orgService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 根据角色id获取用户列表
     * @param roleId
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<User> getUserByRoleId(int roleId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 根据机构id获取用户列表
     * @param orgId
     * @return
     */
    @Override
    public List<User> searchUserByOrgId(int orgId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("org_id",orgId);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 修改个人信息
     * @param dataDto
     * @return
     */
    @Override
    public int updatePersonalData(PersonalDataDto dataDto) {
        String username = UserUtil.getCurrentUser();
        if (!username.equals(dataDto.getUsername())) {
            return -1;
        }
        User user = this.baseMapper.selectById(username);
        user.setName(dataDto.getName());
        user.setPhone(dataDto.getPhone());
        user.setAvatar(dataDto.getAvatar());
        user.setRemark(dataDto.getRemark());
        user.setEmail(dataDto.getEmail());
        return this.baseMapper.updateById(user);
    }

    /**
     * 修改用户
     * @param updateDto
     * @return
     */
    @Override
    public int updateUser(UserUpdateDto updateDto) {
        User user = this.baseMapper.selectById(updateDto.getUsername());
        user.setName(updateDto.getName());
        user.setPhone(updateDto.getPhone());
        user.setRoleId(updateDto.getRoleId());
        user.setOrgId(updateDto.getOrgId());
        Org org = orgService.getById(updateDto.getOrgId());
        user.setOrgName(org.getName());
        user.setRemark(updateDto.getRemark());
        user.setEmail(updateDto.getEmail());
        return this.baseMapper.updateById(user);
    }

    /**
     * 修改个人密码
     * @param dto
     * @return
     */
    @Override
    public int updatePersonalPassword(PasswordDto dto) {
        String oldPassword = new String(Base64.getDecoder().decode(dto.getOldPassword()));
        String newPassword = new String(Base64.getDecoder().decode(dto.getNewPassword()));
        String username = UserUtil.getCurrentUser();
        User user = this.baseMapper.selectById(username);
        boolean matches = passwordEncoder.matches(oldPassword, user.getPassword());
        if (matches) {
            user.setPassword(passwordEncoder.encode(newPassword));
            return this.baseMapper.updateById(user);
        }
        return -1;
    }

    /**
     * 根据用户名获取可操作的机构id
     * @param username
     * @return
     */
    @Override
    public List<Integer> getOperableOrg(String username) {
        User user = this.baseMapper.selectById(username);
        Integer orgId = user.getOrgId();
        return orgService.getPuisneOrgIds(orgId);
    }

    /**
     * 分页查询用户列表
     * @param searchDto
     * @return
     */
    @Override
    public Page<User> searchUsers(UserSearchDto searchDto) {
        Page<User> page = new Page<>(searchDto.getPageNumber(), searchDto.getPageSize());
        QueryWrapper<User> wrapper =new QueryWrapper<>();
        if (searchDto.getOrgIds() != null) wrapper.in("org_id",searchDto.getOrgIds());
        if (searchDto.getStatus() != null) wrapper.in("status",searchDto.getStatus());
        String searchParam = searchDto.getSearchParam();
        if(StringUtils.isNotEmpty(searchParam)){
            wrapper.like("name", searchParam)
                    .or().like("phone",searchParam)
                    .or().like("username",searchParam);
        }
        wrapper.ne("username","maintain");
        return this.page(page, wrapper);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @Override
    public User getUserInfo() {
        return this.getById(UserUtil.getCurrentUser());
    }

    @Override
    public List<UrlInfo> getUserPermission(String username) {
        return this.getBaseMapper().getUserPermission(username);
    }

    @Override
    public List<UrlInfo> getExcludeUrl() {
        return baseMapper.getExcludeUrl();
    }


}
